﻿using Questionnaire.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Questionnaire.Service
{
    public class AccountService
    {
        private QuestionnaireContext _dbContext;

        public AccountService()
        {
            _dbContext = new QuestionnaireContext();
        }
        public User Login(string account,string password)
        {
            var user = _dbContext.User.SingleOrDefault(u => u.Account.Equals(account));
            if (CheckLogin(user))
                return user;
            else
                return null;
        }

        public void Register(User user)
        {
            if (CheckRegister(user.Account))
            {
                User userItem = new User()
                {
                    Account = user.Account,
                    Password = user.Password,
                    CreateTime = DateTime.Now,
                    NickName = user.NickName,
                    HaedPortrait = user.HaedPortrait,
                };
                _dbContext.User.Add(userItem);
                _dbContext.SaveChanges();
            }           
        }   
        public bool CheckLogin(User user)
        {
            return user != null ? true : false;
        }
        public bool CheckRegister(string account)
        {
            var user = _dbContext.User.SingleOrDefault(u => u.Account.Equals(account));
            return user == null ? true : false;
        }
    }
}
